<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="name" :label="$t('lawsuitFlow.name')">
        <el-input v-model="dataForm.name" :placeholder="$t('lawsuitFlow.name')"/>
      </el-form-item>
      <el-form-item v-if="dataForm.parentId !== '0'" prop="days" :label="$t('lawsuitFlow.days')">
<!--        <el-input v-model="dataForm.days" :placeholder="$t('lawsuitFlow.days')"/>-->
        <el-input-number v-model="dataForm.days"  :min="1" :label="$t('lawsuitFlow.days')"/>
      </el-form-item>
      <el-form-item prop="status" :label="$t('lawsuitFlow.status')">
        <el-switch
                v-model="dataForm.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-text="$t('status1')"
                :inactive-text="$t('status0')"
                :active-value="activeValue"
                :inactive-value="inactiveValue">
        </el-switch>
      </el-form-item>
      <el-form-item prop="sort" :label="$t('dict.sort')">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="1" :label="$t('sort')"/>
      </el-form-item>
      <el-form-item prop="remark" :label="$t('dict.remark')">
        <el-input v-model="dataForm.remark" :placeholder="$t('dict.remark')"/>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { debounce } from 'lodash'
export default {
  data () {
    return {
      visible: false,
      inactiveValue:0,
      activeValue:1,
      dataForm: {
        id: '',
        parentId: '0',
        name: '',
        status: 1,
        days: 1,
        sort: 1,
        remark: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        days: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$axios.get(`/arbitrationFlow/info/${this.dataForm.id}`).then(res => {
        this.dataForm = {
          ...this.dataForm,
          ...res
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http.json('/arbitrationFlow/addOrUpdate',this.dataForm).then(res => {
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
